package com.heima.admin.controller;

import com.heima.admin.dto.ChannelDto;
import com.heima.admin.entity.AdChannel;
import com.heima.admin.service.IAdChannelService;
import com.heima.common.dto.ResponseResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/v1/channel")
public class AdChannelController {

    // 注入服务接口
    @Autowired
    private IAdChannelService channelService;

    /**
     * 根据名称模糊查询分页列表
     *
     * @param dto
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "根据名称模糊查询分页列表", notes = "author:mcm")  // value指名称  notes 备注
    @ApiImplicitParam(name = "dto", value = "查询对象", required = true, dataType = "ChannelDto")
    public ResponseResult findListByName(@RequestBody ChannelDto dto) {
        return channelService.findListByName(dto);
    }

    @PostMapping("save")
    @ApiOperation(value = "保存频道", notes = "author:mcm")  // value指名称  notes 备注
    @ApiImplicitParam(name = "channel", value = "频道对象", required = true, dataType = "AdChannel")
    public ResponseResult saveChannel(@RequestBody AdChannel channel){
        channel.setCreatedTime(new Date());
        return channelService.saveChannel(channel);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改频道", notes = "author:mcm")
    public ResponseResult updateChannel(@RequestBody AdChannel adChannel){
        channelService.updateById(adChannel);
        return ResponseResult.okResult();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除频道", notes = "author:mcm")
    public ResponseResult deleteChannel(@PathVariable Integer id){
       return channelService.deleteChannel(id);
    }

    @GetMapping("/channels")
    @ApiOperation(value = "频道列表", notes = "author:mcm")
    public ResponseResult findChannels(){
        List<AdChannel> list = channelService.list();
        return ResponseResult.okResult(list);
    }
}